﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Data;

namespace PracticeProject
{
    public partial class GoalViewForm : Form
    {
        Goal goal;
        List<Activity> RelatedActs;
        List<Goal> subgoals;
        Goal parentGoal;
        public GoalViewForm()
        {
            InitializeComponent();
        }
        public GoalViewForm(Goal g)
        {
            InitializeComponent();
            goal = g;
            
        }

        void SetView()
        {
            lblDescription.Text = goal.Description;
            LblExpDate.Text = goal.ExpectedEndingTime.ToShortDateString();
            LblRealDate.Text = goal.RealEndingTime == new DateTime() ? "Doesn't reached" : goal.RealEndingTime.ToShortDateString();
            txtComments.Text = goal.Comments;
            
            switch (goal.Priority)
            {
                case 0:
                    lblPriority.Text = "High";
                    break;
                case 1:
                    lblPriority.Text = "Normal";
                    break;
                case 2:
                    lblPriority.Text = "Low";
                    break;
            }
            if (goal.IsDone)
                menuSetToDone.Enabled = false;
            RelatedActs = goal.GetActivities();
            pnlActs.Controls.Clear();
            for (int i = 0; i < RelatedActs.Count; i++)
            {
                LinkLabel l = new LinkLabel();
                l.Text = RelatedActs[i].Description;
                l.ForeColor = Color.Black;
                l.Location = new Point(10, 17 * i);
                l.Click += new EventHandler(l_Click);
                pnlActs.Controls.Add(l);
            }

            subgoals = goal.GetSubElements();
            pnlGoals.Controls.Clear();
            for (int i = 0; i < subgoals.Count; i++)
            {
                LinkLabel l = new LinkLabel();
                l.Text = subgoals[i].Description;
                l.ForeColor = Color.Black;
                l.Location = new Point(10, 17 * i);
                l.Click+=new EventHandler(l_Click1);
                pnlGoals.Controls.Add(l);
            }
            parentGoal = goal.GetParent();
            if (parentGoal != null)
            {
                lblParent.Text = parentGoal.Description;
                lblParent.Enabled = true;

            }
            else
            {
                lblParent.Text = "none";
                lblParent.Enabled = false;

            }
        }

        void l_Click1(object sender, EventArgs e)
        {
            int i = pnlGoals.Controls.IndexOf((Control)sender);
            GoalViewForm f = new GoalViewForm(subgoals[i]);
            f.ShowDialog();
            SetView();
        }

        void l_Click(object sender, EventArgs e)
        {
            int i = pnlActs.Controls.IndexOf((Control)sender);
            ActViewForm f = new ActViewForm(RelatedActs[i]);
            f.ShowDialog();
            SetView();
        }

        private void GoalViewForm_Load(object sender, EventArgs e)
        {
            SetView();
            

        }


        private void menuItem6_Click(object sender, EventArgs e)
        {
            ElementCollection elements = ElementCollection.OpenCollectionFromXml();
            elements.DeleteGoal(goal);
            elements.SaveCollectionToXml();
            this.Close();
        }

        private void menuItem5_Click(object sender, EventArgs e)
        {
            Adding_Form f = new Adding_Form(goal);
            f.ShowDialog();
            goal = f.goal;
            SetView();
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            SetingToDoneGoal f = new SetingToDoneGoal();
            if (f.ShowToSetDate(goal))
            {

                ElementCollection elements = ElementCollection.OpenCollectionFromXml();

                elements.EditGoal(goal);
                elements.SaveCollectionToXml();
                SetView();
            }
        }

        private void TxtDescription_GotFocus(object sender, EventArgs e)
        {
           
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            Adding_Form f = new Adding_Form(goal.ID);
            f.ShowDialog();
            SetView();
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            Adding_Activity_Form f = new Adding_Activity_Form(goal.ID);
            f.ShowDialog();
            SetView();
        }

        private void linkLabel1_Click(object sender, EventArgs e)
        {
            GoalViewForm f = new GoalViewForm(parentGoal);
            f.ShowDialog();
            SetView();
        }

    }
}